﻿<UserControl
	x:Name="This"
	x:Class="Poet.Views.PaletteBoxView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:views="clr-namespace:Poet.Views">
	<UserControl.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="..\Resources\XamlStore.xaml"/>
			</ResourceDictionary.MergedDictionaries>
		</ResourceDictionary>
	</UserControl.Resources>
	<Grid>
		<ComboBox 
            MinWidth="32"
		    Name="ComboBox"
		    IsEditable="True"
            SelectedValuePath="Fill"
		    Text="{Binding SampleBrush, ElementName=This, TargetNullValue=''}"
		    ItemsSource="{x:Static views:PaletteBoxView.DefaultPalette}">
			<ComboBox.ItemTemplate>
				<DataTemplate>
					<Grid>
						<Grid.ColumnDefinitions>
							<ColumnDefinition Width="Auto"/>
							<ColumnDefinition Width="*"/>
						</Grid.ColumnDefinitions>

						<Rectangle
                            Grid.Column="0"
                            RadiusX="2" RadiusY="2"
                            Width="12" Height="12" 
                            Stroke="DarkGray"
                            VerticalAlignment="Center" 
                            StrokeThickness="1"
                            Fill="{Binding Fill}"/>

						<TextBlock Grid.Column="1" Text="{Binding Name}" Margin="4,1"/>
					</Grid>
				</DataTemplate>
			</ComboBox.ItemTemplate>
			<ComboBox.Template>
				<ControlTemplate TargetType="ComboBox">
					<Grid>

						<ToggleButton 
                            Name="ToggleButton" 
                            Template="{StaticResource ComboBoxToggleButton}" 
                            Grid.Column="2" 
                            Focusable="false"
                            IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                            ClickMode="Press"/>

						<ContentPresenter
                            Name="ContentSite"
                            IsHitTestVisible="False" 
                            Content="{TemplateBinding SelectionBoxItem}"
                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                            Margin="3,3,23,3"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left"/>

						<TextBox 
                            Name="PART_EditableTextBox"
                            Style="{x:Null}" 
                            Text="{TemplateBinding Text}"
                            HorizontalAlignment="Left" 
                            VerticalAlignment="Center" 
                            Margin="19,3,23,3"
                            Focusable="True" 
                            Background="Transparent"
                            Visibility="Hidden"
                            IsReadOnly="{TemplateBinding IsReadOnly}">
							<TextBox.Template>
								<ControlTemplate>
									<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" Width="{TemplateBinding Width}"/>
								</ControlTemplate>
							</TextBox.Template>
						</TextBox>

						<Popup
                            Name="Popup"
                            AllowDrop="True"
                            Placement="Bottom"
                            IsOpen="{TemplateBinding IsDropDownOpen}"
                            AllowsTransparency="True" 
                            Focusable="False"
                            PopupAnimation="Slide">
							<Grid 
                                Name="DropDown"
                                SnapsToDevicePixels="True"                                                 
                                MinWidth="{TemplateBinding ActualWidth}"
                                MaxHeight="{Binding Height, ElementName=DropDownBorder}">
								<Border 
                                    x:Name="DropDownBorder"
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    BorderBrush="{StaticResource SolidBorderBrush}"
                                    BorderThickness="1">
									<StackPanel>
										<Border CornerRadius="2 2 0 0" Background="{StaticResource NormalBorderBrush}">
                                            <Grid>
                                                <Grid.Resources>
                                                    <Style TargetType="TextBox">
                                                        <Setter Property="MaxWidth" Value="30"/>
                                                        <Setter Property="FontSize" Value="9"/>
                                                        <Setter Property="VerticalContentAlignment" Value="Center"/>
                                                        <Setter Property="Margin" Value="2 1 2 2"/>
                                                    </Style>
                                                    <Style TargetType="TextBlock">
                                                        <Setter Property="VerticalAlignment" Value="Center"/>
                                                        <Setter Property="Margin" Value="2 1 2 2"/>
                                                    </Style>
                                                    <Style TargetType="ScrollBar">
                                                        <Setter Property="Orientation" Value="Horizontal"/>
                                                        <Setter Property="Width" Value="100"/>
                                                        <Setter Property="Margin" Value="2"/>
                                                    </Style>
                                                </Grid.Resources>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition Width="Auto"/>
                                                    <ColumnDefinition/>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition/>
                                                    <RowDefinition/>
                                                    <RowDefinition/>
                                                </Grid.RowDefinitions>

                                                <TextBlock
                                                    Grid.Column="0" Grid.Row="0" 
                                                    Text="{Localizing Opacity, Tail={Localizing Brush, StringFormat=' ({0})', Transformer={x:Static LocalizingManager.DefaultWithToLower}}}"/>
                                                <TextBlock
                                                    Grid.Column="0" Grid.Row="1" 
                                                    Visibility="{Binding ElementName=This, Path=BrushMode, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"
                                                    Text="{Localizing Thickness}"/>
                                                <TextBlock
                                                    Grid.Column="0" Grid.Row="2" 
                                                    Visibility="{Binding ElementName=This, Path=BrushMode, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"
                                                    Text="{Localizing CornerRadius}"/>

                                                <TextBox Grid.Column="1" Grid.Row="0" Width="32" Text="{Binding BrushOpacity, ElementName=This, StringFormat=F2}"/>
                                                <TextBox 
                                                    Grid.Column="1" Grid.Row="1" Width="32"
                                                    Visibility="{Binding ElementName=This, Path=BrushMode, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"
                                                    Text="{Binding Thickness, ElementName=This, StringFormat=F2}"/>
                                                <TextBox 
                                                    Grid.Column="1" Grid.Row="2" Width="32"
                                                    Visibility="{Binding ElementName=This, Path=BrushMode, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"
                                                    Text="{Binding CornerRadius, ElementName=This, StringFormat=F2}"/>

                                                <ScrollBar 
                                                    Grid.Column="2" Grid.Row="0"
                                                    Orientation="Horizontal" Minimum="0" Maximum="1" SmallChange="0.1" Value="{Binding BrushOpacity, ElementName=This, FallbackValue=0.5}"/>
                                                <ScrollBar
                                                    Grid.Column="2" Grid.Row="1"
                                                    Visibility="{Binding ElementName=This, Path=BrushMode, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"
                                                    Orientation="Horizontal" Minimum="0" Maximum="4" SmallChange="0.25" Value="{Binding Thickness, ElementName=This, FallbackValue=0.5}"/>
                                                <ScrollBar
                                                    Grid.Column="2" Grid.Row="2"
                                                    Visibility="{Binding ElementName=This, Path=BrushMode, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"
                                                    Minimum="0" Maximum="8" SmallChange="0.5" Value="{Binding CornerRadius, ElementName=This, FallbackValue=0.5}"/>
                                                
                                            </Grid>
										</Border>
										<ScrollViewer Margin="2" MaxHeight="{TemplateBinding MaxDropDownHeight}" SnapsToDevicePixels="True">
											<VirtualizingStackPanel Name="ItemsPanel" IsItemsHost="True" IsVirtualizing="True" KeyboardNavigation.DirectionalNavigation="Contained" />
										</ScrollViewer>
									</StackPanel>
								</Border>
							</Grid>
						</Popup>
					</Grid>

					<ControlTemplate.Triggers>
						<Trigger Property="HasItems" Value="false">
							<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
						</Trigger>
						<Trigger Property="IsEnabled" Value="false">
							<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
						</Trigger>
						<Trigger Property="IsGrouping" Value="true">
							<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
						</Trigger>
						<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
							<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
							<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
						</Trigger>
						<Trigger Property="IsEditable"  Value="true">
							<Setter Property="IsTabStop" Value="false"/>
							<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
							<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
						</Trigger>
					</ControlTemplate.Triggers>

				</ControlTemplate>
			</ComboBox.Template>
		</ComboBox>

		<Canvas Width="14" Height="14" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="4">
			<Rectangle Width="14" Height="14" HorizontalAlignment="Left" Stroke="DimGray" RadiusX="0" RadiusY="0" StrokeThickness="1">
				<Rectangle.Fill>
					<DrawingBrush Viewport="0,0,0.5,0.5" TileMode="Tile">
						<DrawingBrush.Drawing>
							<DrawingGroup>
								<GeometryDrawing Brush="White">
									<GeometryDrawing.Geometry>
										<RectangleGeometry Rect="0,0,100,100" />
									</GeometryDrawing.Geometry>
								</GeometryDrawing>

								<GeometryDrawing>
									<GeometryDrawing.Geometry>
										<GeometryGroup>
											<RectangleGeometry Rect="0,0,50,50" />
											<RectangleGeometry Rect="50,50,50,50" />
										</GeometryGroup>
									</GeometryDrawing.Geometry>
									<GeometryDrawing.Brush>
										<LinearGradientBrush>
											<GradientStop Offset="0.0" Color="Black" />
											<GradientStop Offset="1.0" Color="Gray" />
										</LinearGradientBrush>
									</GeometryDrawing.Brush>
								</GeometryDrawing>
							</DrawingGroup>
						</DrawingBrush.Drawing>
					</DrawingBrush>
				</Rectangle.Fill>

				<!--<Rectangle RadiusX="2" RadiusY="2" Opacity="0.5"
                        Width="12" Height="12"  Fill="{Binding SampleBrush, ElementName=This}">

            </Rectangle>-->
			</Rectangle>
			<Polygon Points="0,0 0,14, 14,0" Fill="{Binding Brush, ElementName=This}" />
			<Polygon Points="14,14 0,14, 14,0" Fill="{Binding SampleBrush, ElementName=This}"/>
			<!--<Line X1="14" Y1="0" X2="0" Y2="14" Stroke="DimGray"/>
            <Line X1="0" Y1="0" X2="0" Y2="14" Stroke="DimGray"/>
            <Line X1="0" Y1="14" X2="14" Y2="14" Stroke="DimGray"/>
            <Line X1="14" Y1="14" X2="14" Y2="0" Stroke="DimGray"/>
            <Line X1="0" Y1="0" X2="14" Y2="0" Stroke="DimGray"/>-->
		</Canvas>
	</Grid>
</UserControl>
